package com.lwldjx.auth.application.controller;

import com.alibaba.fastjson.JSON;

import com.google.common.base.Preconditions;
import com.lwldjx.auth.application.convert.AuthRoleDTOConverter;

import com.lwldjx.auth.application.dto.AuthRoleDTO;
import com.lwldjx.auth.common.entity.Result;
import com.lwldjx.auth.domain.entity.AuthRoleBO;
import com.lwldjx.auth.domain.service.AuthRoleDomainService;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/role/")
@Slf4j
public class RoleController {
    @Resource
    private AuthRoleDomainService authRoleDomainService;

    @RequestMapping("add")
    public Result<Boolean> register(@RequestBody AuthRoleDTO authRoleDTO) {
        try{
            if (log.isInfoEnabled()) {
                log.info(" RoleController.add.dto:{}",  JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()), "角色key不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()), "角色名称不能为空");
            AuthRoleBO authRoleBo= AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            Boolean result =authRoleDomainService.add(authRoleBo);
            return Result.ok(result);
        }catch (Exception e){
            log.error("AuthRoleController.add.error:{}",e.getMessage(),e);
            return Result.fail("增加角色失败"+e.getMessage());
        }
    }
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
        try{
            if (log.isInfoEnabled()) {
                log.info(" RoleController.update.dto:{}",  JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(),"ID不能为空");
            AuthRoleBO authRoleBo= AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            Boolean result =authRoleDomainService.update(authRoleBo);
            return Result.ok(result);
        }catch (Exception e){
            log.error("AuthRoleController.update.error:{}",e.getMessage(),e);
            return Result.fail("更新角色失败"+e.getMessage());
        }
    }
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO) {
        try{
            if (log.isInfoEnabled()) {
                log.info(" RoleController.delete.dto:{}",  JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(),"ID不能为空");
            AuthRoleBO authRoleBo= AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            Boolean result =authRoleDomainService.delete(authRoleBo);
            return Result.ok(result);
        }catch (Exception e){
            log.error("AuthRoleController.delete.error:{}",e.getMessage(),e);
            return Result.fail("删除角色失败"+e.getMessage());
        }
    }
    @RequestMapping("deleteById")
    public Result<Boolean> deleteById(@RequestBody AuthRoleDTO authRoleDTO) {
        try{
            if (log.isInfoEnabled()) {
                log.info(" RoleController.deleteById.dto:{}",  JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(),"ID不能为空");
            AuthRoleBO authRoleBo= AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            Boolean result =authRoleDomainService.deleteById(authRoleBo);
            return Result.ok(result);
        }catch (Exception e){
            log.error("AuthRoleController.deleteById.error:{}",e.getMessage(),e);
            return Result.fail("删除角色失败"+e.getMessage());
        }
    }
}

